<?
class HotelDetailForm extends Form
{
    function HotelDetailForm()
    {
        Form::Form('HotelDetailForm');
        $this->link_css("default.css");
        $this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_css("style/paging.css");
        $this->link_css('style/msgBoxLight.css');
        $this->link_css("style/reset.min.css");
        $this->link_css("style/front-main-menu.css");
        $this->link_css("style/front-breadcrum.css");
        $this->link_css("style/front-detail-hotel.css");
        $this->link_css("style/front-detail-feature-hotel.css");
        $this->link_css("style/jquery.lightbox-0.5.css");
        $this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        $this->link_js("javascript/jquery/jquery.msgBox.js");
        $this->link_js("javascript/jquery/jquery.lightbox-0.5.js");
        $this->link_js("javascript/jquery.dd.min.js");
        $this->link_js('javascript/hoteldetail.js');

        $id = (int)Url::get('id');
        if ($id) {
            $this->hotel = Db_Hotel::getHotelByIdFromRedis($id);
            CGlobal::$website_title = $this->hotel['name'];
        }

        if (!$id || !$this->hotel || $this->hotel['status'] != 1) {
            Url::redirect(WEB_DIR);
        }
    }

    function draw()
    {
        Db_Location::get_cities();
        global $display;

        if (User::is_login()) {
            $display->add('login', 1);
        }

        //check giá phòng
        if (!isset($_GET['from_date']) && !isset($_GET['to_date'])) {
            $display->add('is_choice_date', false);
            $rooms = Db_Room::getRoomsByHotelId($this->hotel['id']);
            $display->add('check_rates', false);
        } else {
            $from_date = Url::get('from_date', '');
            $display->add('from_date', $from_date);
            $to_date = Url::get('to_date', '');
            $display->add('to_date', $to_date);
            if (isset($_GET['from_date']) && isset($_GET['to_date'])) {
                $get_from_date = explode('-', $from_date);
                $int_from_date = mktime("0", "0", "0", $get_from_date[1], $get_from_date[0], $get_from_date[2]);
                $get_to_date = explode('-', $to_date);
                $int_to_date = mktime("0", "0", "0", $get_to_date[1], $get_to_date[0], $get_to_date[2]);
            }

            if ($from_date == '' || $to_date == '' || $int_from_date >= $int_to_date) {
                $display->add('is_choice_date', false);
                $rooms = Db_Room::getRoomsByHotelId($this->hotel['id']);
                $display->add('check_rates', false);
            } else {
                $display->add('is_choice_date', true);
                $display->add('check_rates', true);
                $so_ngay = ($int_to_date - $int_from_date) / 60 / 60 / 24;
                $display->add('so_ngay', $so_ngay);
                $rooms = Db_Room::getRoomsAvailableByHotelId($this->hotel['id'], $int_from_date, $int_to_date);
            }
        }

        foreach ($rooms as &$r) {
            if (isset($r['imageDefault']) && $r['imageDefault'] != "") {
                $room_imgs = Db_Image::selectImages('id, img_name, img_url, img_server', '', "id IN (" . $r['imageDefault'] . ") ORDER BY find_in_set(id, '(" . $r['imageDefault'] . ")')");
                foreach ($room_imgs as $ri) {
                    $r["img_server"] = $ri["img_server"];
                    $r["img_url"] = $ri["img_url"];
                }
            }else{
                $r['imageDefault'] = "";
            }
        }

        $display->add('rooms', $rooms);
        $display->add('sizerooms', sizeof($rooms));
//        $this->hotel['district'] = (!empty($this->hotel['district_id'])) ? Db_Location::getLocaltionNameById($this->hotel['district_id']) : '';
        $city = Db_Location::getCityNotDistrict($this->hotel['city_id']);
        $this->hotel['city'] = $city['name'];
        $this->hotel['city_slug'] = $city['bkname'];

        if ($this->hotel['images']) {
            $imgs = Db_Image::selectImages('id, img_name, img_url, img_server', '', "id IN (" . $this->hotel['images'] . ") ORDER BY find_in_set(id, '(" . $this->hotel['images'] . ")')");
            $display->add('hotel_imgs', $imgs);
        }
        $order_imgs = explode(',', $this->hotel['images']);
        /*$checkedimg = array();
        foreach ($order_imgs as &$o) {
            if (Db_Image::checkimg($o)) {
                $checkedimg[] = $o;
            }
        }*/
        $display->add('order_imgs', $order_imgs);
        $display->add('size_order_imgs', sizeof($order_imgs));
        $display->add('hotel_images', $this->hotel['images']);

        $display->add("features_data", $this->get_data_features(explode(',', $this->hotel['filter_ids'])));

        //review
        $point = array();
        $count = array();
        $avg = array();
        $cus = Db_HotelReview::$cus_types;
        $rate = Db_HotelReview::$rate_types;

        for ($i = 0; $i < sizeof($cus); $i++) {
            $point_cus = array();
            $count_cus = array();
            $avg_cus = array();

            for ($j = 0; $j < sizeof($rate); $j++) {
                $key_point = 'rating_' . $i . '_' . $j . '_point';
                array_push($point_cus, $this->hotel[$key_point]);

                $key_count = 'rating_' . $i . '_' . $j . '_count';
                array_push($count_cus, $this->hotel[$key_count]);

                $key_avg = 'rating_' . $i . '_' . $j . '_average';
                array_push($avg_cus, $this->hotel[$key_avg]);
            }
            array_push($point, $point_cus);
            array_push($count, $count_cus);
            array_push($avg, $avg_cus);
        }

        $display->add('cus', $cus);
        $display->add('rate', $rate);
        $display->add('point', $point);
        $display->add('count', $count);
        $display->add('avg', $avg);
        $display->add('feel', Db_HotelReview::getFeelByScore($avg[0][0]));

        //Set cookie lưu lại các khách sạn đã xem
        if(REDIS_ON == 1){
            $redis = RedisPhp::getInstance('hotel');
            if (!empty($_COOKIE["recent_hotel"])) {
                $ck = explode('|', $_COOKIE["recent_hotel"]);
                if (!in_array($this->hotel['id'], $ck)){
                    $cookie = $this->hotel['id'] . '|' . $_COOKIE["recent_hotel"];
                    if ($redis->EXISTS(HOTEL_VIEW.$this->hotel['id']) == 1){
                        $last = $redis->ZREVRANGE(HOTEL_VIEW.$this->hotel['id'], 0, -1);
                        $index = $last[0] + 1;
                    }else{
                        $index = 1;
                    }
                    $redis->ZADD(HOTEL_VIEW.$this->hotel['id'], TIME_NOW, $index);
                }else{
                    $cookie = $_COOKIE["recent_hotel"];
                }
            } else {
                $cookie = $this->hotel['id'];
            }
            $time = 60 * 60 * 24 * 365 + TIME_NOW;
            setcookie("recent_hotel", $cookie, $time, "/");
        }
        $display->add("WEB_ROOT", WEB_ROOT);
        $display->add("WEB_DIR", WEB_DIR);
        $display->add('hotel', $this->hotel);
        $display->output('HotelDetail');
    }

    function get_data_features($data_used)
    {
        $features = array();
        $features_group = Db_Filter::getHotelFilersGroup();
        foreach ($features_group as $value) {
            $id = $value['id'];
            $features_temp = Db_Filter::getHotelFilersByGroupId($id);
            foreach ($features_temp as $v) {
                $features[$id][$v['id']]['name'] = $v['name'];
                if (in_array($v['id'], $data_used)) {
                    $features[$id][$v['id']]['has_service'] = true;
                } else {
                    $features[$id][$v['id']]['has_service'] = false;
                }
            }
        }
        return array("features_group" => $features_group, "features" => $features);
    }
}

?>